<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/12
 * Time: 15:32
 */

namespace app\admin\controller;
use think\Db;

class Common extends AdminController
{
    public function testPage(){
        return $this->fetch('page/test_edit');
    }


    public function upload()
    {
        $imgInfo = array();
        $file = request()->file()['file_upload'];
        if (is_array($file)){
            for($i = 0; $i< count($file); $i++){
                $imgInfo[$i] = $this->saveImage($file[$i]);
            }
        }else{
            $imgInfo = $this->saveImage($file);
        }
        echo json_encode($imgInfo);
    }

    protected function saveImage($file)
    {
        if( $file == null ){
            return null;
        }else{
            // 保存文件
            $res = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($res){
                // 成功上传
                $data['file_id'] = 1;
                $data['status'] = 1;
                $data['origin_file_name'] = $_FILES['file_upload']['name'];
                $data["file_name"] = "http://" . $_SERVER["SERVER_NAME"] . DS . "uploads" . DS  . $res->getSaveName();
                $data["file_path"] = "http://" . $_SERVER["SERVER_NAME"] . DS . "uploads" . DS  . $res->getSaveName();
            }else{
                // 上传失败
                echo $file->getError();
                return null;
            }
            return $data;
        }
    }


}